package com.way.view.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.way.bean.MessageBean;
import com.way.dialer.R;

public class MessageBoxListAdapter extends BaseAdapter {

	private List<MessageBean> mMsgbeanLists;
	private Context mContext;
	private LayoutInflater mLayoutInflater;

	public MessageBoxListAdapter(Context context, List<MessageBean> coll) {
		mContext = context;
		mLayoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mMsgbeanLists = coll;
	}

	public int getCount() {
		return mMsgbeanLists.size();
	}

	public Object getItem(int position) {
		return mMsgbeanLists.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		MessageBean mb = mMsgbeanLists.get(position);
		int itemLayout = mb.getLayoutID();
		ViewHolder holder;
		if (convertView == null
				|| convertView.getTag(R.drawable.ic_launcher + itemLayout) == null) {
			holder = new ViewHolder();
			convertView = mLayoutInflater.inflate(itemLayout, parent, false);
			holder.tvText = (TextView) convertView
					.findViewById(R.id.messagedetail_row_text);
			holder.tvDate = (TextView) convertView
					.findViewById(R.id.messagedetail_row_date);
			convertView.setTag(R.drawable.ic_launcher + itemLayout, holder);
		} else {
			holder = (ViewHolder) convertView.getTag(R.drawable.ic_launcher
					+ itemLayout);
		}
		holder.tvText.setText(mb.getText());
		holder.tvDate.setText(mb.getDate());
		addListener(convertView, mb);
		return convertView;
	}

	static class ViewHolder {
		TextView tvText;
		TextView tvDate;
	}

	public void addListener(View layout_bj, final MessageBean mb) {

		layout_bj.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

			}
		});

		layout_bj.setOnLongClickListener(new OnLongClickListener() {
			public boolean onLongClick(View v) {
				showListDialog(newtan, mb);
				return true;
			}
		});
	}

	private String[] newtan = new String[] { "转发", "复制文本内容", "删除", "查看信息详情" };

	private void showListDialog(final String[] arg, final MessageBean mb) {
		new AlertDialog.Builder(mContext).setTitle("信息选项")
				.setItems(arg, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {

						case 0:
							break;

						case 1:
							ClipboardManager cmb = (ClipboardManager) mContext
									.getSystemService(mContext.CLIPBOARD_SERVICE);
							cmb.setText(mb.getText());
							break;
						case 2:

							break;
						case 3:
							break;
						}
						;
					}
				}).show();
	}
}
